package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* ClipView03
* */
class ClipView04 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            strokeWidth = mStrokeWidth
            color = context.resources.getColor(R.color.color_0F96DF)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    private val mPath2 by lazy {
        Path()
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)

            mPath1.apply {
                /*moveTo(-cx * 3 / 5, -cy * 3 / 5)
                lineTo(-cx * 1 / 5, 0F)
                lineTo(-cx * 3 / 5, cy * 3 / 5)*/

                moveTo(-cx * 3 / 5, -cy * 3 / 5)
                lineTo(cx * 3 / 5, cy * 3 / 5)
            }

            mPath2.apply {
                /*moveTo(cx * 3 / 5, -cy * 3 / 5)
                lineTo(cx * 1 / 5, 0F)
                lineTo(cx * 3 / 5, cy * 3 / 5)*/

                moveTo(cx * 3 / 5, -cy * 3 / 5)
                lineTo(-cx * 3 / 5, cy * 3 / 5)
            }

            mPath0.apply {
                fillType = Path.FillType.INVERSE_WINDING
                //Path.FillType.WINDING---去除两者之间的公共部分，两者之间的非公共部分保留原样，两者之间方向互异;方向相同则不做处理
                //Path.FillType.INVERSE_WINDING---保留两者非公共部分和公共部分，两者之间方向互异;方向相同则不做处理
                addRect(
                    -cx,
                    -cy,
                    cx,
                    cy,
                    Path.Direction.CW
                )

                addPath(mPath1)

                addPath(mPath2)
            }
            drawPath(mPath0, mPaint0)
        }
    }
}